﻿// Entity Framework MapTileStore - http://maptilestore.codeplex.com/
// Copyright © 2012 Majid Qaraee
// Licensed under the Microsoft Public License (Ms-PL)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace MapEntity.Model
{
    public class TileLoad
    {
        public static byte[] DownloadTile(Uri uri)
        {
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);

                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                Stream responseStream = webResponse.GetResponseStream();

                int length = (int)webResponse.ContentLength;
                
                if (length > 0)
                {
                    
                    byte[] buffer = new byte[length];

                    responseStream.Read(buffer, 0, length);

                    return buffer;
                }
            }
            catch
            {
            }
            return null;
        }
    }
}
